package com.hb.gp.util;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Random;
import java.util.StringTokenizer;

public class MsgGenUtil {
	private static int iSerial;
	static {
		iSerial = 0;
	}

	public MsgGenUtil() {
	}

	public static byte[] appendByte(byte abyte0[], byte byte0) {
		byte abyte1[] = new byte[abyte0.length + 1];
		System.arraycopy(abyte0, 0, abyte1, 0, abyte0.length);
		abyte1[abyte0.length] = byte0;
		return abyte1;
	}

	public static String b2bin(byte byte0) {
		String s = Integer.toBinaryString(byte0 & 0xff);
		String s1 = s;
		for (int i = 0; i < 8 - s.length(); i++)
			s1 = "0" + s1;
		return s1;
	}

	public static String b2hex(byte byte0) {
		String s = Integer.toHexString(byte0 & 0xff);
		String s1;
		if (s.length() == 1)
			s1 = "0".concat(s);
		else
			s1 = s;
		return s1.toUpperCase();
	}

	/*
	 * String->byte[]
	 */
	public static byte[] BCD2Bytes(String s) {
		// byte[] tmp = new byte[0];
		byte[] returnBytes;
		int nlen = (s.length() + 1) / 2;
		returnBytes = new byte[nlen];//
		for (int i = 0; i < nlen; i++) {
			// a = (byte) Integer.parseInt(s.substring(2 * i, 2 * i + 2), 16);
			// tmp = appendByte(tmp, a);
			int temp = Integer.parseInt(s.substring(2 * i, 2 * i + 2), 16);
			returnBytes[i] = new Integer(temp).byteValue();

		}
		// return tmp;
		return returnBytes;
	}

	public static String BCDAdd(String bcd, int j) {
		int i = Integer.parseInt(bcd, 16) + j;
		String s = Integer.toHexString(i);
		while (s.length() % 2 == 1 || s.length() < 4)
			s = "0" + s;
		return s;
	}

	public static String buf2hex(byte abyte0[]) {
		String s = new String();
		for (int i = 0; i < abyte0.length; i++)
			s = s.concat(b2hex(abyte0[i]));

		s = s.toUpperCase();
		// s= s.toLowerCase();
		return s;
	}

	public static String buf2hex(byte abyte0[], int len) {
		String s = new String();
		for (int i = 0; i < len; i++)
			s = s.concat(b2hex(abyte0[i]));

		// s = s.toUpperCase();
		s = s.toLowerCase();
		return s;
	}

	public static byte[] concat(byte abyte0[], byte abyte1[]) {
		byte abyte2[] = new byte[abyte0.length + abyte1.length];
		System.arraycopy(abyte0, 0, abyte2, 0, abyte0.length);
		System.arraycopy(abyte1, 0, abyte2, abyte0.length, abyte1.length);
		return abyte2;
	}

	public static byte[] getSerial() {
		iSerial++;
		if (iSerial > 0x7FFFFFFF) {
			iSerial = 0;
			System.out.println(iSerial);
		}
		byte[] target = new byte[4];
		IntToBytes(iSerial, target);
		return target;
	}

	public static byte[] Int2Bytes(int source) {
		byte[] target = new byte[4];
		for (int i = 0; i < 4; i++) {
			target[3 - i] = (byte) ((source >> (i * 8)) & 0x000000FF);
		}
		return target;
	}

	/*
	 * ֻȡ3���ֽ�
	 */
	public static byte[] Int2Bytes3(int source) {
		byte[] target = new byte[3];
		for (int i = 0; i < 3; i++) {
			target[2 - i] = (byte) ((source >> (i * 8)) & 0x000000FF);
		}
		return target;
	}

	/*
	 * ֻȡ5���ֽ�
	 */
	public static byte[] Long2Bytes5(long source) {
		byte[] target = new byte[5];
		for (int i = 0; i < 5; i++) {
			target[4 - i] = (byte) ((source >> (i * 8)) & 0x000000FFl);
		}
		return target;
	}

	/*
	 * ֻȡ�����ֽ�
	 */
	public static byte[] Int2Bytes2(int source) {
		byte[] target = new byte[2];
		for (int i = 0; i < 2; i++) {
			target[1 - i] = (byte) ((source >> (i * 8)) & 0x000000FF);
		}
		return target;
	}

	/*
	 * ֻȡһ���ֽ�
	 */
	public static byte[] Int2Bytes1(int source) {
		byte[] target = new byte[1];

		target[0] = (byte) (source & 0x000000FF);

		return target;
	}

	public static String int2s(int i) {
		String s = "" + i;
		if (i < 10)
			s = "0" + s;
		return s;
	}

	public static byte[] intStr2Bytes(String i) {
		int it = Integer.parseInt(i);
		String s = Integer.toHexString(it);
		if (s.length() % 2 == 1)
			s = "0" + s;
		byte[] rt = BCD2Bytes(s);
		return rt;
	}

	public static void IntToBytes(int source, byte[] target) {
		for (int i = 0; i < 4; i++) {
			target[3 - i] = (byte) ((source >> (i * 8)) & 0x000000FF);
		}
	}

	public static int StrToInt(String scr, byte[] tar) {
		int nLen = scr.length(), sum = 0;
		byte[] middle;
		try {
			middle = scr.getBytes("UTF-8");
		} catch (Exception err) {
			err.printStackTrace();
			return 0;
		}
		for (int i = 0; i < nLen; i++) {
			sum = sum * 10 + (middle[i] - '0');
		}
		IntToBytes(sum, tar);
		return sum;
	}

	// public static void main(String[] args) throws Exception {

	// String a = args[0];
	/*
	 * System.out.println(buf2hex(intStr2Bytes(a)));
	 * System.out.println(buf2hex(now2Bytes()));
	 * System.out.println(now2Bytes());
	 * System.out.println(Integer.parseInt("-1",16));
	 * System.out.println(BCDAdd("7fffffff",0x7fffffff));
	 * System.out.println(buf2hex(getSerial()));
	 * System.out.println(buf2hex(getSerial()));
	 * System.out.println(buf2hex(Int2Bytes(-1)));
	 */
	/*
	 * System.out.println(buf2hex(a.getBytes("UTF-8")));
	 * 
	 * String c = new String(a.getBytes("UTF-8"), "UTF-8");
	 * System.out.println(c); System.out.println(MsgGenUtil.BCDAdd("0007",5));
	 */
	/*
	 * byte[] b=a.getBytes(); byte[] c={(byte)0xff,(byte)0xff,(byte)1111};
	 * String[] d={"my test","����������ֲ��"};
	 * 
	 * System.out.println(b.length+
	 * "   "+Integer.toHexString(Integer.MAX_VALUE)+
	 * "   "+Integer.toHexString(Integer.MIN_VALUE));
	 * System.out.println(MsgGenUtil.buf2hex(a.getBytes()));
	 * System.out.println(MsgGenUtil.buf2hex(c));
	 * System.out.println(MsgGenUtil.makeInt(c)+"   "+Integer.parseInt(a));
	 * System.out.println((Integer.parseInt(a)+1)/2);
	 * System.out.println(Integer.toBinaryString(Integer.parseInt(a)));
	 * 
	 * System.out.println(b2hex((byte)Integer.parseInt("ff",16)));
	 */
	/*
	 * SMS sms=new SMS(SMS.NORMAL_SMS); sms.setOA("+8613701393741",true);
	 * sms.setTPDU("0111"); System.out.println(sms.generate()); byte[]
	 * bsms=BCD2Bytes(sms.generate()); byte[] dsms=BCD2Bytes(sms.genDeliver());
	 * 
	 * System.out.println(buf2hex(dsms));
	 * 
	 * System.out.println("deliver:"+buf2hex(SMMOMT.getTPOA(dsms)));
	 * System.out.println("d-tpud:"+buf2hex(SMMOMT.getDeliverTPUD(dsms)));
	 * 
	 * System.out.println(buf2hex(BCD2Bytes(sms.oa)));
	 * System.out.println(buf2hex(bsms));
	 * System.out.println("SUBMIT:"+buf2hex(SMMOMT.getTPDA(bsms)));
	 * System.out.println
	 * ("xin�й�:nihao "+replace("xin�й�:nihao�й�:","�й�",toUsc2("�й�"))); /*
	 * System.out.println(d.length); //
	 * System.out.println(Character.getNumericValue(d[1].charAt(1)));
	 * System.out.println(buf2hex(d[1].getBytes()));
	 * System.out.println(buf2hex(BCD2Bytes
	 * (buf2hex(d[1].getBytes("Unicode")))));
	 */

	// }

	public static int makeInt(byte abyte0[]) {
		return Integer.parseInt(buf2hex(abyte0), 16);
	}

	public static byte[] now2Bytes() {
		String s = "";
		Calendar now = new GregorianCalendar();
		Date trialTime = new Date();
		now.setTime(trialTime);

		s += int2s(now.get(Calendar.MONTH) + 1);
		s += int2s(now.get(Calendar.DATE));
		s += int2s(now.get(Calendar.HOUR_OF_DAY));
		s += int2s(now.get(Calendar.MINUTE));
		s += int2s(now.get(Calendar.SECOND));

		return intStr2Bytes(s);
	}

	public static byte[] padding(byte abyte0[], byte byte0, int i) {
		byte abyte1[] = new byte[abyte0.length + i];
		System.arraycopy(abyte0, 0, abyte1, 0, abyte0.length);
		for (int j = 0; j < i; j++)
			abyte1[j + abyte0.length] = byte0;

		return abyte1;
	}

	public static String replace(String src, String find, String rp) {
		int i = src.indexOf(find);
		if (i < 0)
			return src;
		String a = src.substring(0, i);
		String b = src.substring(i + find.length(), src.length());
		return a + rp + b;
	}

	public static String replaceAll(String src, String find, String rp) {
		String a = "";
		String b = "";
		if (src.startsWith(find))
			a = rp;
		if (src.endsWith(find))
			b = rp;
		StringTokenizer st = new StringTokenizer(src, find);
		String t, rt;
		rt = "";
		while (st.hasMoreTokens()) {
			t = st.nextToken();
			rt = rt + t + rp;
		}
		rt = a + rt.substring(0, rt.lastIndexOf(rp)) + b;
		return rt;
	}

	public static String toUsc2(String s) throws Exception {
		try {
			String a = buf2hex(s.getBytes("UnicodeBig"));
			if (a.length() > 4)
				return a.substring(4);
			else
				return a;
		} catch (Exception ex) {
			return "";
		}
	}

	public static void fillbyte(byte[] a, byte b) {
		for (int i = 0; i < a.length; i++)
			a[i] = b;

	}

	public static int getByteTrimLen(byte src[]) {
		try {
			String temp = new java.lang.String(src, "UTF-8");
			return temp.trim().length();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return -1;
	}

	public static int getIndexByteLen(byte src[], int offset) {
		char zero = 0x0000;
		int j = 0;
		try {
			String temp = new java.lang.String(src, "UTF-8");
			if (src[offset] != 0x00) {
				for (j = offset; j < temp.length(); j++) {
					if ((j > 0) && (temp.charAt(j) == zero)
							&& (temp.charAt(j - 1) != zero))
						break;
				}
				if (j < temp.length())
					return temp.substring(offset, j).length() + 1;
				else
					return 1;
			} else
				return 1;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return -1;
	}

	public static String toGBK(String src) {
		byte[] temp = null;
		try {
			temp = src.getBytes("utf-16");
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return "80" + buf2hex(temp).replaceFirst("FEFF", "");

	}

	public static String random() {
		Random r = new Random();
		String rs = "" + r.nextInt(9999);
		while (rs.length() < 16) {
			rs = rs + r.nextInt(9);
		}
		return rs;
	}

	public static void main(String args[]) {
		String testString = "0F";
		byte[] s = BCD2Bytes(testString);
		for (int i = 0; i < s.length; i++) {
			System.out.println(s[i]);
		}
		System.out.println(buf2hex(s));
	}
}
